//
//  DDTabBarController.m
//  Dingdong
//
//  Created by 程强 on 16/11/21.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDTabBarController.h"
#import "SYTabBar.h"
#import "SYTabBarItem.h"

@interface DDTabBarController ()<SYTabBarDelegate>
@property (strong, nonatomic) SYTabBar *bar;
@property (strong, nonatomic) NSMutableArray *itemAry;
@end

@implementation DDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *titleAry = @[@"Home",@"Sorts",@"Shopping Cart",@"My"];
    NSArray *imgAry = @[LoadImage(@"tab1_s"),LoadImage(@"tab2_s"),LoadImage(@"tab3_s"),LoadImage(@"tab4_s")];
    NSArray *selectImgAry = @[LoadImage(@"tab1"),LoadImage(@"tab2"),LoadImage(@"tab3"),LoadImage(@"tab4")];
    
    self.itemAry = [[NSMutableArray alloc] init];
    
    for (int i = 0; i<titleAry.count; i++) {
        SYTabBarItem *item = [[SYTabBarItem alloc]initWithFrame:CGRectMake(0+i*ScreenWidth/4, 0, ScreenWidth/4, 50) title:titleAry[i] selectedImage:imgAry[i] unselectedImage:selectImgAry[i]];
        item.selectedTitleColor = RGBColor(233,36,37);
        item.unselectedTitleColor = RGBColor(153, 153, 153);
        if (i == 0) {
            [item setSelected:YES];
        }
        item.syTag = i;
//        if (i == 2) {
//            item.badgeValue = @"15";
//        }
        [_itemAry addObject:item];
    }
    
    self.bar = [[SYTabBar alloc]initWithSYTabBarItems:_itemAry];
    [_bar setShowDividingLine:NO];
    _bar.tabBarDelegate = self;
    self.bar.backgroundColor = [UIColor whiteColor];
    [self.tabBar insertSubview:_bar atIndex:0];
    
    for (UIView *view in [self.tabBar subviews]) {
        NSString *str = NSStringFromClass([view class]);
        if ([str isEqualToString:@"UITabBarButton"]||[str isEqualToString:@"UITabBarItem"]) {
            [view removeFromSuperview];
        }
    }
}
- (BOOL)tabBar:(SYTabBar *)tabBar willSelectItem:(SYTabBarItem *)item
{
    [tabBar setSelectedItem:item];
    return NO;
}
- (void)tabBar:(SYTabBar *)tabBar didSelectItem:(SYTabBarItem *)item
{
    [self setSelectedIndex:item.syTag];
    UINavigationController *naviVC = self.viewControllers[item.syTag];
    [naviVC.topViewController.navigationController popToRootViewControllerAnimated:NO];
}
- (void)setCartBadge:(NSString *)cartNum
{
    [self.bar setBadgeValue:cartNum atIndex:2];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
